package com.fatdetection.demo.util;

import com.alibaba.fastjson.JSON;
import com.fatdetection.demo.bean.RstJson;
import io.jsonwebtoken.Claims;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TokenErrorMsg {
    private final HttpServletResponse response;
    private static final RstJson rstJson = new RstJson();
    public TokenErrorMsg(HttpServletResponse response){
        this.response = response;
        rstJson.setCode(400);
        rstJson.setData("");
    }

    public  TokenErrorMsg noToken(){
        rstJson.setMsg("token 不存在");
        return this;
    }
    public TokenErrorMsg tokenOutDate(){
        rstJson.setMsg("token 已过期");
        return this;
    }
    public TokenErrorMsg writeTo(String msg){
        rstJson.setMsg(msg);
        return this;
    }
    public TokenErrorMsg tokenNotValid(){
        rstJson.setMsg("token 不合法");
        return this;
    }
    public void sendMsg(){
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        try {
            response.getWriter().println(JSON.toJSON(rstJson));
            response.getWriter().flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
